﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.Xml.Linq;
using System.Web.Services;
using Telerik.Web.UI;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GBMS.APP_UI.UserControls;

namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class BGM_Project_Expense_New : System.Web.UI.Page
    {
        private IProject projectBLL;
        private ComboBLL comboBLL = new ComboBLL();
        private IExpenseGroupBLL ExpGrp = new ExpenseGroupBLL();
        private ISourceBLL sourceBLL = new SourceBLL();
        private string strType;

        protected void Page_Load(object sender, EventArgs e)
        {
            strType = Request.QueryString["type"].ToString();

            if (strType.Length > 0)
            {
                projectBLL = Project_Factory.GetProjectFactory(strType);
                Session["type"] = strType;
            }

            if (!IsPostBack)
            {
                //ucExpenseDetail = (ExpenseDetail)Page.LoadControl("../UserControls/ExpenseDetail.ascx");
                lbSourceSeq.Text = Request.QueryString["source"].ToString();

                if (!Object.Equals(Session["ProjectID"], null))
                {
                    lbProjectID.Text = Session["ProjectID"].ToString();
                    //Session.Remove("ProjectID");
                }
                else
                {
                    //================= test ====================
                    lbProjectID.Text = "150";
                    //===========================================
                }

                if (!Object.Equals(Session["ProjectName"], null))
                {
                    litTitle.Text = Session["ProjectName"].ToString()+ " [" + sourceBLL.Search(Convert.ToInt32(lbSourceSeq.Text))[0].Name + "]";
                }

                if (Request.QueryString["test"] != null)
                {
                    //================ test ==================
                    if (Request.QueryString["test"] == "1" && Request.QueryString["expid"] != null)
                    {
                        Session["Approve"] = false;
                        LoadRBLBgmExpGrpSeq();
                        rblBgmExpGrpSeq.SelectedValue = Request.QueryString["expid"];
                        rdShowAll.Checked = true;
                        rdShowAll_CheckedChanged(null, null);
                    }
                    //========================================
                }
                else
                {
                    LoadRBLBgmExpGrpSeq();
                    LoadData();
                }
           }
        }

        public void LoadData()
        {
            bool b = (rdShowAll.Checked);
            DataTable dtb = projectBLL.ListProjectExpenseGrid(lbProjectID.Text
                , lbSourceSeq.Text, rblBgmExpGrpSeq.SelectedValue, b);

            gridExpense.DataSource = dtb;
            gridExpense.DataBind();

            dtb = projectBLL.GetDataProjectExpenseGroup(lbProjectID.Text, lbSourceSeq.Text);
            for (int i = 0; i < dtb.Rows.Count; i++)
            {
                for (int j = 0; j < rblBgmExpGrpSeq.Items.Count; j++)
                {
                    if (dtb.Rows[i]["bgm_exp_grp_seq"].ToString() == rblBgmExpGrpSeq.Items[j].Value)
                    {
                        rblBgmExpGrpSeq.Items[j].Text = dtb.Rows[i]["bgm_exp_grp_name"].ToString() + " : [<span class='span'>" + Convert.ToDouble(dtb.Rows[i]["total_amount"]).ToString("#,###.00") + "</span>]";
                    }
                }
            }
        }

        private void LoadRBLBgmExpGrpSeq()
        {
            rblBgmExpGrpSeq.DataSource = ExpGrp.Search(-1);
            rblBgmExpGrpSeq.DataBind();

            rblBgmExpGrpSeq.SelectedIndex = 1;
        }

        protected void gridExpense_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                var item = e.Item as GridDataItem;
                string strBgmExpSeq = WebString(item["value"].Text);
                string strBgmExpGrpSeq = rblBgmExpGrpSeq.SelectedValue;
                string strBgmProjSeq = lbProjectID.Text;
                string strBgmProjExpSeq = WebString(item["bgm_proj_exp_seq"].Text);
                string strBgmSourceSeq = lbSourceSeq.Text;
                string strIndex = item.ItemIndex.ToString();
                Label lbTotalAmount = (Label)item["total_amount"].FindControl("lbTotalAmount");
                string strTotalAmount = WebString(lbTotalAmount.Text);
                string strIcmResItemSeq = WebString(item["icm_res_item_seq"].Text);
                string strUnitPrice = WebString(item["unit_price"].Text);
                string strUnitQty = WebString(item["unit_qty"].Text);
                //string strDesc = WebString(item["descr"].Text);
                string Args = "";
                ImageButton btn = (ImageButton)e.Item.FindControl("DetailControl");

                Args = strBgmExpGrpSeq
                    + "|" + strBgmExpSeq
                    + "|" + strBgmProjExpSeq
                    + "|" + strBgmProjSeq
                    + "|" + strBgmSourceSeq
                    + "|" + strIndex
                    + "|" + strTotalAmount
                    + "|" + strIcmResItemSeq
                    + "|" + strUnitPrice
                    + "|" + strUnitQty;

                btn.OnClientClick = string.Format("OpenRadWindow('{0}')", Args);
                btn.Visible = !(bool)Session["Approve"];
            }
        }

        protected void gridExpense_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Filter")
            {
                LoadData();
            }
        }

        protected void rdShowSelected_CheckedChanged(object sender, EventArgs e)
        {
            rdShowAll.Checked = false;
            rdShowSelected.ForeColor = Color.OrangeRed;
            rdShowAll.ForeColor = Color.Black;
            LoadData();
        }

        protected void rdShowAll_CheckedChanged(object sender, EventArgs e)
        {
            rdShowSelected.Checked = false;
            rdShowSelected.ForeColor = Color.Black;
            rdShowAll.ForeColor = Color.OrangeRed;
            LoadData();
        }

        protected void OnAjaxUpdate(object sender, ToolTipUpdateEventArgs args)
        {
            if (gridExpense.Items.Count > int.Parse(args.Value))
            {
                GridDataItem item = gridExpense.Items[int.Parse(args.Value)];
                string strBgmExpSeq = WebString(item["value"].Text);
                string strBgmExpGrpSeq = rblBgmExpGrpSeq.SelectedValue;
                string strBgmProjSeq = lbProjectID.Text;
                string strBgmProjExpSeq = WebString(item["bgm_proj_exp_seq"].Text);
                string strBgmSourceSeq = lbSourceSeq.Text;
                string strIndex = args.Value;
                Label lbTotalAmount = (Label)item["total_amount"].FindControl("lbTotalAmount");
                string strTotalAmount = WebString(lbTotalAmount.Text);
                string strIcmResItemSeq = WebString(item["icm_res_item_seq"].Text);
                string strUnitPrice = WebString(item["unit_price"].Text);
                string strUnitQty = WebString(item["unit_qty"].Text);

                Session["ExpenseDetail"] = strBgmExpGrpSeq
                    + "|" + strBgmExpSeq
                    + "|" + strBgmProjExpSeq
                    + "|" + strBgmProjSeq
                    + "|" + strBgmSourceSeq
                    + "|" + strIndex
                    + "|" + strTotalAmount
                    + "|" + strIcmResItemSeq
                    + "|" + strUnitPrice
                    + "|" + strUnitQty;
                this.UpdateToolTip(args.UpdatePanel);
            }
        }

        private void UpdateToolTip(UpdatePanel panel)
        {
            ExpenseDetail ucExpenseDetail = new ExpenseDetail();
            ucExpenseDetail = (ExpenseDetail)Page.LoadControl("../UserControls/ExpenseDetail.ascx");
            panel.ContentTemplateContainer.Controls.Clear();
            panel.ContentTemplateContainer.Controls.Add(ucExpenseDetail);
            //ucExpenseDetail.LoadData();
        }

        protected void rblBgmExpGrpSeq_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        protected string WebString(string strValue)
        {
            string strResult = "";

            strResult = strValue.Replace("&nbsp;", "").Replace("\r\n", " ");

            return strResult;
        }

        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            LoadData();
        }
    }
}
